package vn.edu.nuce.bean;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.TabChangeEvent;

import vn.edu.nuce.model.LichDangKy;
import vn.edu.nuce.service.LichDangKyService;
import vn.edu.nuce.utility.Utility;

@ManagedBean(name="quanLyLichSuBean")
@ViewScoped
public class QuanLyLichSuBean {
	@ManagedProperty(value = "#{LichDangKyService}")
	LichDangKyService lichDangKyService;

	private Date date;
	private Date fromDate;
	private Date toDate;
	private int thangSelected;
	private int namSelected;
	private int namSelected2;
	private String strMonHoc;
	private String ngayHienThi;
	private int tab = 1;
	private boolean reportHuy=false;
	List<Integer> thangList;
	List<Integer> namList;
	List<LichDangKy> lichDangKyList;

	public QuanLyLichSuBean() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		thangList = new ArrayList<Integer>();
		namList = new ArrayList<Integer>();
		int year = Calendar.getInstance().get(Calendar.YEAR);
		for (int i = 0; i < 12; i++) {
			namList.add(year--);
			thangList.add(i + 1);
		}
		try {
			Date dateWithoutTime = sdf.parse(sdf.format(new Date()));
			date = dateWithoutTime;
			fromDate = dateWithoutTime;
		} catch (ParseException e) {
			date = new Date();
			fromDate = new Date();
			e.printStackTrace();
		}
		ngayHienThi = Utility.formatDate(date);
	}

	public void handleDateSelect(SelectEvent event) {
		date = (Date) event.getObject();
		getListFromDate();
	}

	public void handleWeekSelect(SelectEvent event) {
		fromDate = (Date) event.getObject();
		getListFromWeek();
	}

	

	private void getListFromDate() {
		lichDangKyList = getLichDangKyService().getLichDangKyByDateAndUser(
				date, "","");
		ngayHienThi = Utility.formatDate(date);
	}

	private void getListFromWeek() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(fromDate);
		cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
		fromDate = cal.getTime();
		cal.add(Calendar.DATE, 6);
		toDate = cal.getTime();
		lichDangKyList = getLichDangKyService().getLichDangKyBy2DateAndUser(
				fromDate, toDate, "","");
		ngayHienThi = Utility.formatDate(fromDate) + " - " + Utility.formatDate(toDate);
	}

	public void getListFromMonth() {
		Calendar cal = Calendar.getInstance();
		cal.set(namSelected, thangSelected - 1, 1);
		fromDate = cal.getTime();
		cal.set(namSelected, thangSelected - 1,
				cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		toDate = cal.getTime();
		lichDangKyList = getLichDangKyService().getLichDangKyBy2DateAndUser(
				fromDate, toDate, "","");
		ngayHienThi = thangSelected + "/" + namSelected;
	}

	public void getListFromYear() {
		Calendar cal = Calendar.getInstance();
		cal.set(namSelected2, 0, 1);
		fromDate = cal.getTime();
		cal.set(namSelected2, 11, 31);
		toDate = cal.getTime();
		lichDangKyList = getLichDangKyService().getLichDangKyBy2DateAndUser(
				fromDate, toDate, "","");
		ngayHienThi = namSelected2 + "";
	}

	public void getListFromMonHoc(){
		lichDangKyList = getLichDangKyService().getLichDangKyTheoMonHocAndUser(strMonHoc,"","");
		ngayHienThi = "";
	}

	public void onTabChange(TabChangeEvent event) {
		reportHuy = false;
		if (event.getTab().getId().equals("tab1")) {
			getListFromDate();
			tab = 1;
		} else if (event.getTab().getId().equals("tab2")) {
			getListFromWeek();
			tab = 2;
		} else if (event.getTab().getId().equals("tab3")) {
			getListFromMonth();
			tab = 3;
		} else if (event.getTab().getId().equals("tab4")) {
			getListFromYear();
			tab = 4;
		} else if (event.getTab().getId().equals("tab5")) {
			getListFromMonHoc();
			tab = 5;
		}
	}
	
	public void hienThiDangKyHuy(){
		reportHuy = true;
		if (tab==1){
			lichDangKyList = getLichDangKyService().getLichDangKyByDateAndUser(
					date, "","-1");
		} else if (tab==2){
			lichDangKyList = getLichDangKyService().getLichDangKyBy2DateAndUser(
					fromDate, toDate, "","-1");
		} else if (tab==3){
			lichDangKyList = getLichDangKyService().getLichDangKyBy2DateAndUser(
					fromDate, toDate, "","-1");
		} else if (tab==4){
			lichDangKyList = getLichDangKyService().getLichDangKyBy2DateAndUser(
					fromDate, toDate, "","-1");
		} else if (tab==5){
			lichDangKyList = getLichDangKyService().getLichDangKyTheoMonHocAndUser(strMonHoc,"","-1");
		}
	}
	public void export() {
		try {
			HSSFWorkbook workBook = new HSSFWorkbook();
			Row row = null;
			Cell cell = null;
			Sheet sheet = workBook.createSheet();
			row = sheet.createRow(0);
			cell = row.createCell(0);
			cell.setCellValue("Thông tin đăng ký phòng máy "+ngayHienThi);
			row = sheet.createRow(1);
			cell = row.createCell(0);
			cell.setCellValue("STT");
			cell = row.createCell(1);
			cell.setCellValue("Phòng");
			cell = row.createCell(2);
			cell.setCellValue("Tiết");
			cell = row.createCell(3);
			cell.setCellValue("Giáo viên");
			cell = row.createCell(4);
			cell.setCellValue("Tiêu đề");
			cell = row.createCell(5);
			cell.setCellValue("Mô tả");
			cell = row.createCell(6);
			cell.setCellValue("Yêu cầu");
			cell = row.createCell(7);
			cell.setCellValue("Trạng thái");
			if (reportHuy){
				cell = row.createCell(8);
				cell.setCellValue("Lý do hủy");
			}

			int size = lichDangKyList.size();
			LichDangKy lichDangKy;
			for (int rowIndex = 0; rowIndex < size; rowIndex++) {
				lichDangKy = lichDangKyList.get(rowIndex);
				row = sheet.createRow(rowIndex+2);
				cell = row.createCell(0);
				cell.setCellValue(rowIndex+1);
				cell = row.createCell(1);
				cell.setCellValue(lichDangKy.getTenPhongMay());
				cell = row.createCell(2);
				cell.setCellValue(lichDangKy.getMoTaTietHoc());
				cell = row.createCell(3);
				cell.setCellValue(lichDangKy.getTenGiaoVien());
				cell = row.createCell(4);
				cell.setCellValue(lichDangKy.getTieuDe());
				cell = row.createCell(5);
				cell.setCellValue(lichDangKy.getMoTa());
				cell = row.createCell(6);
				cell.setCellValue(lichDangKy.getYeuCau());
				cell = row.createCell(7);
				cell.setCellValue(LichDangKyBean.convertStatus(lichDangKy.getStatus()+""));
				if (reportHuy){
					cell = row.createCell(8);
					cell.setCellValue(lichDangKy.getLyDoHuy());
				}
			}

			String fileName = "lichgiangday.xls";
			FileOutputStream fos = new FileOutputStream(fileName);
			workBook.write(fos);
			fos.close();
			String contentType = "application/vnd.ms-excel";
			FacesContext fc = FacesContext.getCurrentInstance();
			HttpServletResponse response = (HttpServletResponse) fc
					.getExternalContext().getResponse();
			response.reset();
			response.setHeader("Content-disposition", "attachment; filename="
					+ fileName);
			response.setContentType(contentType);
			BufferedInputStream bis = new BufferedInputStream(
					new FileInputStream(fileName));
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int c;
			while ((c = bis.read()) > -1) {
				baos.write(c);
			}
			bis.close();
			OutputStream out = response.getOutputStream();
			out.write(baos.toByteArray());
			out.flush();
			out.close();
			fc.responseComplete();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public List<LichDangKy> getLichDangKyList() {
		if (lichDangKyList == null) {
			lichDangKyList = getLichDangKyService().getLichDangKyByDateAndUser(
					fromDate, "","");
		}
		return lichDangKyList;
	}

	public void setLichDangKyList(List<LichDangKy> lichDangKyList) {
		this.lichDangKyList = lichDangKyList;
	}

	public Date getFromDate() {
		return fromDate;
	}

	public void setFromDate(Date fromDate) {
		this.fromDate = fromDate;
	}

	public Date getToDate() {
		return toDate;
	}

	public void setToDate(Date toDate) {
		this.toDate = toDate;
	}

	public List<Integer> getThangList() {
		return thangList;
	}

	public void setThangList(List<Integer> thangList) {
		this.thangList = thangList;
	}

	public List<Integer> getNamList() {
		return namList;
	}

	public void setNamList(List<Integer> namList) {
		this.namList = namList;
	}

	public LichDangKyService getLichDangKyService() {
		return lichDangKyService;
	}

	public void setLichDangKyService(LichDangKyService lichDangKyService) {
		this.lichDangKyService = lichDangKyService;
	}

	public String getNgayHienThi() {
		return ngayHienThi;
	}

	public void setNgayHienThi(String ngayHienThi) {
		this.ngayHienThi = ngayHienThi;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public int getThangSelected() {
		return thangSelected;
	}

	public void setThangSelected(int thangSelected) {
		this.thangSelected = thangSelected;
	}

	public int getNamSelected() {
		return namSelected;
	}

	public void setNamSelected(int namSelected) {
		this.namSelected = namSelected;
	}

	public int getNamSelected2() {
		return namSelected2;
	}

	public void setNamSelected2(int namSelected2) {
		this.namSelected2 = namSelected2;
	}

	public String getStrMonHoc() {
		return strMonHoc;
	}

	public void setStrMonHoc(String strMonHoc) {
		this.strMonHoc = strMonHoc;
	}

	public int getTab() {
		return tab;
	}

	public void setTab(int tab) {
		this.tab = tab;
	}

	public boolean isReportHuy() {
		return reportHuy;
	}

	public void setReportHuy(boolean reportHuy) {
		this.reportHuy = reportHuy;
	}


}
